package com.mdlib.droid.base;

import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.view.View;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mdlib.droid.R;
import com.mdlib.droid.util.core.ImageLoader;
import com.mdlib.droid.widget.CommonTopBar;


/**
 * Created by librabin on 16/11/15.
 */

public abstract class BaseTitleFragment extends BaseAppFragment {

    private CommonTopBar mTitlebar;
    private View mVTitleLine;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_base_title;
    }

    @Override
    protected void onBindViewBefore(View rootView) {
        super.onBindViewBefore(rootView);
        ViewStub stub = (ViewStub) rootView.findViewById(R.id.layout_content);
        stub.setLayoutResource(getContentLayoutId());
        stub.inflate();
    }


    protected abstract
    @LayoutRes
    int getContentLayoutId();

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        mTitlebar = (CommonTopBar) rootView.findViewById(R.id.top_bar);
        mVTitleLine = rootView.findViewById(R.id.v_title_line);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getBarHeight();
        }
    }


    public BaseTitleFragment setTitleText(String title) {
        if (mTitlebar != null) {
            mTitlebar.setTextCenter(title);
        }

        return this;
    }

    public BaseTitleFragment setLineShow(boolean isShow) {
        if (isShow) {
            mVTitleLine.setVisibility(View.VISIBLE);
        } else {
            mVTitleLine.setVisibility(View.GONE);
        }


        return this;
    }

    public BaseTitleFragment setCommonTitle(String titleStr) {
        return setCommonTitle(titleStr, R.color.white);
    }


    public BaseTitleFragment setCommonTitle(@StringRes int titleId) {
        return setCommonTitle(getActivity().getResources().getString(titleId), R.color.white);
    }

    public BaseTitleFragment setCommonTitle(@StringRes int titleId, @ColorRes int titleColorId) {
        return setCommonTitle(getActivity().getResources().getString(titleId), R.color.white);
    }

    public BaseTitleFragment setCommonTitle(String titleStr, @ColorRes int titleColorId) {
        return setTitle(titleStr, R.color.black)
                .setTitleBgColor(titleColorId)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
    }

    public BaseTitleFragment setTitle(@StringRes int titleId, @ColorRes int titleColorId) {
        return setTitle(getActivity().getResources().getString(titleId), titleColorId);
    }


    public BaseTitleFragment setTitle(String titleText, @ColorRes int titleColorId) {
        if (mTitlebar != null) {
            TextView titleField = (TextView) mTitlebar.findViewById(R.id.tv_title_center);
            titleField.setText(titleText);
            titleField.setTextColor(getActivity().getResources().getColor(titleColorId));
            titleField.setTextSize(18);
            mTitlebar.setVisibility(View.VISIBLE);
        }
        return this;
    }

    public BaseTitleFragment withoutReturn() {
        if (mTitlebar != null) {
            mTitlebar.setShowImgLeft(false);
        }
        return this;
    }


    public BaseTitleFragment setTitleText(@StringRes int titleId) {
        if (mTitlebar != null) {
            mTitlebar.setTextCenter(getActivity().getResources().getString(titleId));
        }
        return this;
    }

    public BaseTitleFragment setTitleColor(@ColorRes int titleColorId) {
        if (mTitlebar != null) {
            TextView titleText = (TextView) mTitlebar.findViewById(R.id.tv_title_center);
            titleText.setTextColor(getActivity().getResources().getColor(titleColorId));
        }
        return this;
    }

    public BaseTitleFragment setTitleBgColor(@ColorRes int bgColorId) {
        if (mTitlebar != null) {
            ImageView bgView = (ImageView) mTitlebar.findViewById(R.id.iv_title_bg);
            if (bgView != null) {
                if (bgColorId == 0) {
                    bgView.setBackgroundColor(getActivity().getResources().getColor(R.color.black));
                } else {
                    bgView.setBackgroundColor(getActivity().getResources().getColor(bgColorId));
                }
            }
//            mTitlebar.setBackgroundColor(getResources().getColor(bgColorId));
        }

        return this;
    }


    public BaseTitleFragment setImgLeftBg(@DrawableRes int id) {
        if (mTitlebar != null) {
            mTitlebar.setImgLeft(id);
            mTitlebar.setShowImgLeft(true);
        }

        return this;
    }

    public BaseTitleFragment setRightBtn(String rightTextStr, @ColorRes int tvColorId, View.OnClickListener clickListener) {
        if (mTitlebar != null) {
            mTitlebar.setShowRight(true);
            FrameLayout rigthLayout = (FrameLayout) mTitlebar.findViewById(R.id.fl_title_right);
            rigthLayout.setOnClickListener(clickListener);
            TextView rightText = (TextView) mTitlebar.findViewById(R.id.tv_title_right);
            rightText.setTextColor(getActivity().getResources().getColor(tvColorId));
            rightText.setVisibility(View.VISIBLE);
            rightText.setText(rightTextStr);
            rightText.setTextSize(12);

        }


        return this;
    }


    public BaseTitleFragment setRightBtn(View.OnClickListener clickListener) {
        if (mTitlebar != null) {
            mTitlebar.setShowRight(true);
            FrameLayout rigthLayout = (FrameLayout) mTitlebar.findViewById(R.id.fl_title_right);
            rigthLayout.setOnClickListener(clickListener);

        }
        return this;
    }

    public BaseTitleFragment setRightBtnV2(String text) {
        if (mTitlebar != null) {
            mTitlebar.setShowRight(true);
            TextView rightText = (TextView) mTitlebar.findViewById(R.id.tv_title_right);
            rightText.setVisibility(View.VISIBLE);
            rightText.setText(text);
            rightText.setTextSize(12);

        }
        return this;
    }

    public BaseTitleFragment setRightBtnV3(int resId, View.OnClickListener clickListener) {
        if (mTitlebar != null) {
            mTitlebar.setShowRight(true);
            FrameLayout rigthLayout = (FrameLayout) mTitlebar.findViewById(R.id.fl_title_right);
            rigthLayout.setOnClickListener(clickListener);
            ImageView rightIv = (ImageView) mTitlebar.findViewById(R.id.iv_title_right);
            rightIv.setVisibility(View.VISIBLE);
            ImageLoader.displayByRes(mActivity, resId, rightIv);
        }
        return this;
    }

    public BaseTitleFragment setLetBtn(String content, @ColorRes int tvColorId, View.OnClickListener clickListener) {
        if (mTitlebar != null) {
            FrameLayout leftLayout = (FrameLayout) mTitlebar.findViewById(R.id.fl_title_left);
            leftLayout.setOnClickListener(clickListener);
            TextView leftTv = (TextView) mTitlebar.findViewById(R.id.tv_title_left);
            leftTv.setTextColor(getActivity().getResources().getColor(tvColorId));
            leftTv.setText(content);
            leftTv.setTextSize(12);
            leftTv.setVisibility(View.VISIBLE);
            ImageView leftIv = (ImageView) mTitlebar.findViewById(R.id.iv_title_left);
            leftIv.setVisibility(View.GONE);
        }
        return this;
    }

    /**
     * 设置左边图片 监听器
     */
    public BaseTitleFragment setImgLeftListener(View.OnClickListener listener) {
        if (mTitlebar != null) {
            FrameLayout leftLayout = (FrameLayout) mTitlebar.findViewById(R.id.fl_title_left);
            leftLayout.setOnClickListener(listener);
        }
        return this;
    }

    public BaseTitleFragment hideLeftButtonV2() {
        if (mTitlebar != null) {
            FrameLayout leftLayout = (FrameLayout) mTitlebar.findViewById(R.id.fl_title_left);
            leftLayout.setVisibility(View.INVISIBLE);
        }
        return this;
    }

    public BaseTitleFragment hideRightButton() {
        if (mTitlebar != null) {
            FrameLayout leftLayout = (FrameLayout) mTitlebar.findViewById(R.id.fl_title_right);
            leftLayout.setVisibility(View.INVISIBLE);
        }
        return this;
    }

    public BaseTitleFragment showRightButton() {
        if (mTitlebar != null) {
            FrameLayout leftLayout = (FrameLayout) mTitlebar.findViewById(R.id.fl_title_right);
            leftLayout.setVisibility(View.VISIBLE);
        }
        return this;
    }

    public BaseTitleFragment hideTitle() {
        if (mTitlebar != null) {
            mTitlebar.setVisibility(View.INVISIBLE);
        }
        return this;
    }

    public BaseTitleFragment hideLeftButton() {
        if (mTitlebar != null) {
            mTitlebar.setShowImgLeft(false);
        }

        return this;
    }

    /**
     * 获取状态栏高度——方法1
     */
    private void getBarHeight() {
        int statusBarHeight1 = -1;
//获取status_bar_height资源的ID
        int resourceId = getActivity().getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getActivity().getResources().getDimensionPixelSize(resourceId);
        }
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mTitlebar.getLayoutParams();
        layoutParams.setMargins(0, statusBarHeight1, 0, 0);
        mTitlebar.setLayoutParams(layoutParams);
    }
}
